<?php


namespace App\Http\Controllers\Admin\ProductGroup;

use \App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;

class ProductGroupController extends BaseController
{
    /**
     * @api {get} /product/group/list 积分商城列表
     * @apiVersion 0.1.1
     * @apiName 积分商城组列表
     * @apiGroup 积分商城管理
     *
     * @apiParam {number} page      页数，非必传
     * @apiParam {number} perPage     页数，非必传
     * @apiSuccessExample 成功响应
     * {
     * "id": 6,
     * "name": "luck",
     * "sort": 10,
     * "status": 1,
     * "update_time": "2020-12-14 10:19:14",
     * "on_sale": 0,
     * "add_time": "2020-12-23 10:01:23",
     * "active": 0
     * }
     * }
     * }
     *
     */

    public function getProductGroupListApi(Request $request)
    {
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);
        $productGroupList = app('repo_product_group')->getProductGroupList($page, $perPage);
        foreach ($productGroupList as $productGroup) {
            $productGroup['add_time'] = date('Y-m-d h:i:s', $productGroup['add_time']);
            $productGroup['update_time'] = date('Y-m-d h:i:s', $productGroup['update_time']);
        }

        return Response::sendData($productGroupList);
    }

    /**
     * @api {get} /product/group/detail/list 积分商城详情列表
     * @apiVersion 0.1.1
     * @apiName 积分商城组详情列表
     * @apiGroup 积分商城详情管理
     *
     * @apiParam {number} group_id      商品组ID
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": [
     * {
     * "id": 2,
     * "name": "商品组1的第二个商品",
     * "group_id": 1,
     * "sort": 2,
     * "reward_id": 2,
     * "cost_score": 20,
     * "day_limit": 2,
     * "add_time": 22,
     * "reward_data": [
     * {
     * "id":1
     * "reward_id": 27,
     * "product_id": 1001,
     * "name": null,
     * "number": 11,
     * "weight": 2
     * },
     * {
     * "id":1
     * "reward_id": 27,
     * "product_id": 1002,
     * "name": null,
     * "number": 8,
     * "weight": 3
     * }
     * ]
     * }
     * ]
     * }
     * "meta": {
     * "page（页数）": 1,
     * "limit（每页数量）": 10,
     * "next（下一页）": "http://admin.rh/gift/list?=2",
     * "total（总数）": 37
     * }
     * }
     * }
     *
     */

    public function getProductGroupInfoApi(Request $request)
    {
        $groupId = $request->input('group_id', 1);
        $productGroupList = app('repo_product_group')->getProductGroupInfoList($groupId);
        $allWeight = 0;
        $return_data = [];
        $i = 0;
        foreach ($productGroupList as $productGroup) {
            $return_data[$i] = [
                'id'         => $productGroup->id,
                'name'       => $productGroup->name,
                'group_id'   => $productGroup->group_id,
                'sort'       => $productGroup->sort,
                'reward_id'  => $productGroup->reward_id,
                'cost_score' => $productGroup->cost_score,
                'day_limit'  => $productGroup->day_limit,
                'add_time'   => date('Y-m-d h:i:s', $productGroup->add_time),
                'type'       => $productGroup->type,
                'remark'     => $productGroup->remark,
                'number'     => $productGroup->number,
            ];
            $rewardDatas = $productGroup->reward_data;
            if (isset($rewardDatas['bag_type']) && $rewardDatas['bag_type'] == 2) {
                if (isset($rewardDatas['list']) && !empty($rewardDatas['list'])) {
                    $allWeight = array_sum(array_column($rewardDatas['list'], 'weight'));
                }
            }
            $list = $productGroup->reward_data['list'] ?? [];
            foreach ($list as $key => $value) {
                if (isset($rewardDatas['bag_type']) && $rewardDatas['bag_type'] == 2) {
                    $list[$key]['weight'] = round($value['weight'] * 100 / $allWeight);
                }
            }
            $return_data[$i]['reward_data'] = $list;
            $i++;
        }

        return Response::sendData(array_values($return_data));
    }

    /**
     * @api {put} /product/group/add 添加积分商城组
     * @apiVersion 0.1.1
     * @apiName 更新积分商城
     * @apiGroup 积分商城管理
     *
     * @apiParam {number} id     序号,必传 新增传0
     * @apiParam {list} product_details   商品列表 [{id:1,name(商品):1,reward_id(商品ID):1,cost_score(兑换所需要的积分):1,day_limit(1每天限制次数):1,type:1(1道具包2道具)}]
     * @apiParam {string} name    积分商城组名字
     * @apiParam {number} sort    优先级
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 98010  添加积分商城失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 98010,
     *          "message": "errorMsg"
     *      }
     *
     */

    public function addProductGroup(Request $request)
    {
        $productGroup = $request->only(['id', 'name', 'sort']);
        $productDetails = $request->input('product_details', []);
        app('repo_product_group')->addProductGroup($productGroup, $productDetails);

        return Response::sendData(true);
    }

    /**
     * @api {put} /product/group/del 下架积分商城
     * @apiVersion 0.1.1
     * @apiName 下架积分商城
     * @apiGroup 积分商城管理
     *
     * @apiParam {list} ids    [1,2,3]
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 98010  下架积分商城失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 98010,
     *          "message": "errorMsg"
     *      }
     *
     */

    public function delProductGroupApi(Request $request)
    {
        $ids = $request->input('ids', [1]);
        app('repo_product_group')->delProductGroup($ids);

        return Response::sendData(true);
    }

    public function updateProductGroupApi(Request $request)
    {
        $id = $request->input('id', 1);
        $type = $request->input('type', 1);

        app('repo_product_group')->updateProductGroup($id, $type);

        return Response::sendData(true);
    }

    /**
     * @api {get} /product/group/exchange/log 积分兑换记录
     * @apiVersion 0.1.1
     * @apiName 积分兑换记录
     * @apiGroup 积分商城管理
     *
     * @apiParam {number} page      页数
     * @apiParam {number} perPage     页数
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "current_page": 1,
     * "data": [
     * {
     * "id": 25,
     * "user_id": 547900,
     * "group_id": 1,
     * "detail_id": 1,
     * "reward_id": 2,
     * "add_time": 2020-12-54 04:55:54,
     * "cost_score": 10,
     * "user_data": {
     * "user_id": 547900,
     * "nickname": "啊吧啊吧啊吧啊吧啊b"
     * },
     * "reward_data": {
     * "id": 2,
     * "name": "飞龙在天",
     * "type": 2,
     * "picture": ""
     * }
     * }
     * }
     *
     */

    public function getExchangeProductApi(Request $request)
    {
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);

        $exchanges = app('repo_product_group')->getExchangeProduct($page, $perPage, $request->input());
        foreach ($exchanges as $exchange) {
            $exchange['add_time'] = date('Y-m-d H:i:s', $exchange['add_time']);
        }

        return Response::sendData($exchanges);
    }

}
